Read about sql query to find duplicate records in column, The latest news, videos, and discussion topics about sql query to find duplicate records in column from alibabacloud.com
duplication occurs because the table is poorly designed and can be resolved by adding a unique index column.
2. This type of repetition usually requires the first record in the duplicate record to be retained, as follows
Suppose there is a duplicate field of name,address that requires a unique result set for both fields
Select Identity (int,1,1) as Aut
Method 1 Delete yourtable where [id] not in ( Select MAX ([id]) from yourtable Group by (name + Valu) E) Method 2 Delete a from table a LEFT join ( Select (ID) from table GROUP by Name,value ) b on A.id=b.id Where b.id is null Query and delete duplicate records SQL statements
--Query a table to effectively remove duplicate records, UserID as a self-growing primary key, Roleid as a repeating field
The code is as follows
Copy Code
SELECT MIN (UserID) as UserID, Roleid from Tmptable GROUP by RoleidSELECT Roleid from Tmptable GROUP by Roleid (COUNT (*) > 1) to get duplicat
the table design is not weekly. You can add a unique index column.
2. Repeat problems usually require that the first record in the repeat record be retained. The procedure is as follows:
Assume that the duplicate fields are Name and Address. You must obtain the unique result set of the two fields.
select identity(int,1,1) as autoID, * into #Tmp from tableName select min(autoID) as autoID into #Tmp2 from #
duplicate key fields, such as duplicate Name fields, other fields are not necessarily repeated or can be ignored.
1. For the first type of repetition, it is easier to solve.
?
1
Select distinct * from tableName
You can get the result set without repeated records.
If the table needs to delete duplicate
select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
Iv. Supplement
There are more than two duplicate records. One is a completely repeated record, that is, record with all fields already exists. The other is record with
1. -- query the duplicate value of a column (or multiple columns) (only the value of the duplicate record can be found, not the information of the entire record)
-- For example, query records with repeated item_no and item_subno
There is a field "name" in Table A,and the "name" value may be the same between different records,Now you need to query the records in the table, the "name" value duplicates the item;Select Name,count (*) from-A Group by Name has Count (*) > 1If the same gender is also the same large as the following:Select Name,sex,count (*) from-A Group by Name,sex have Count (
duplicate records)-- The premise of such a score is: there must be a non-repeated column, in this example, recno-- For example, query records with duplicate stuidSelect * From stuinfo S1Where recno not in (Select max (recno) from
SQL statement to query duplicate records and delete Repeated Records Search for records with all repeated titles:
Select * From t_info A where (select count (*) from t_info where title = A. Title)> 1) Order by title DESC
I.
Searc
There are often repeated records in the database. sometimes I want to query duplicate records and display them, and sometimes I want to delete them. next I will introduce how to delete and query duplicate
This article describes three SQL statements for querying duplicate records and deleting duplicate records. Each of them uses different methods to illustrate other functions, feasibility, and efficiency.
This article describes three SQL
There is A field ldquo; name rdquo; in Table A, and the value of ldquo; name rdquo; between different records may be the same, now you need to query the records in the table, and the ldquo; name rdquo; value has repeated items; SelectName, Count (*) fromAGroupByNameHavingCount (*) gt; 1. If the gender is also the same, for example, the field "name" exists
1. Find all duplicate records
The code is as follows
Copy Code
SELECT * FROM table Where repeat field in (Select repeating field from table Group by repeating field having Count (*) >1)
2. Filter duplicate records (show only one)
The code is as follows
Copy Code
ROWID records
The code is as follows
Copy Code
Delete from CFAwhere (Businessid,customer) in (select Businessid,customerFrom CFAGROUP BY Businessid,customerHaving count (*) > 1)and rowID not in (select min (rowid)From CFAGROUP BY Businessid,customerHaving count (*) > 1);
Alternatively, use the following simpler and more efficient statements
code is as follows
copy code
DELETE f
No matter what database I use, duplicate data exists. Next I will introduce in detail various methods and Performance Analysis for deleting duplicate records in Oracle tables, for more information, see.
Method:
The Code is as follows:
Copy code
Group by XX having count (*)> 1, rowid, distinct, temporary table, procedure
The followin
Select * from table1
Select * from table2
Select a. *, B. * from table1 a, table2 B where a. id = B. e
-- Query if column X of Table 1 has repeated values, take only oneSelect min (id) as id, B, c from table1 group by B, c order by id asc
-- 1. Search for redundant duplicate records in the Table.
Let's take a look at how you can use it to delete duplicate records in a table:
Copy Code code as follows:
If Exists (Select * FROM Tempdb.Information_Schema.Tables Where table_name like ' #Temp% ')
Drop Table #temp
Create Table #temp ([Id] int, [Name] varchar, [age] int, [SEX] bit default 1)
Go
Insert into #temp ([Id], [Name], [age], [Sex]) Values (1, ' James ', 25,default)
Insert into #t
Single table DistinctMulti-table GROUP byGroup by must be placed before order by and limit, or it will be an errorLet's take a look at the example below:TableID Name1 A1 A3 C4 C5 bThe library structure is probably like this, this is just a simple example, the actual situation will be much more complex.For example, if I want to use a single statement to find all the data that name does not repeat, then you must use distinct to remove the redundant duplicate
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.